<template>
    <div class="pt44">
        <div class="weui-cells weui-cells_form">
            <div class="weui-cell">
                <div class="weui-cell__hd">
                    <label class="weui-label">交易金额</label>
                </div>
                <div class="weui-cell__bd">
                    <div class="ui-input-text ui-body-inherit ui-corner-all ui-shadow-inset"><input  @input="moneyInput" id="moneyInput" class="weui-input" type="tel" placeholder="请输入交易金额"></div>
                </div>
            </div>
        </div>
        <div class="page__bd page__bd_spacing">
            <a @click="saoma" href="javascript:;" v-bind:class="{'weui-btn_disabled':noSaoma}" class="weui-btn  weui-btn_primary">扫码</a>
        </div>

        <div class="js_dialog" id="prompt-dialog" v-show="staffSign">
            <div class="weui-mask"></div>
            <div class="weui-dialog">
                <!-- <div class="weui-dialog__hd"><strong class="weui-dialog__title">弹窗标题</strong></div> -->
                <div class="weui-dialog__bd">进行消费的商户尚未签到，是否现在签到?</div>
                <div class="weui-dialog__ft">
                    <a @click="closeSign" href="javascript:;" class="weui-dialog__btn weui-dialog__btn_default">否</a>
                    <a @click="signAgain" href="javascript:;" class="weui-dialog__btn weui-dialog__btn_primary">是</a>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
// 判断是否有效登入
var cm = require("../components/common.js");
export default  {
    name: 'ScanCode',  // 组件名称
    data () {  // 数据
        return {
          msg: 'ScanCode',
          money:"",
          latitude:"0.00",
          longitude:"0.00",
          noSaoma:true,
          staffSign:false,  // 是否再签到
        }
    },
    methods:{ // 自定义方法，可对data进行处理
        haslogin(msg) {
            if (msg.login_response) {
                this.$store.state.errorMsg = "该账户已在别的地方登录,请重新登入";
                setTimeout(function() {
                    location.href = LoginInit_URL;
                }, 3000);
            }
        },
        moneyInput(){
            var theloanAmount  = $("#moneyInput").val();
            if(/^0\d/.test(theloanAmount)){
              theloanAmount = "0";
            }
            theloanAmount = theloanAmount.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
            theloanAmount = theloanAmount.replace(/^\./g, ""); //验证第一个字符是数字而不是
            theloanAmount = theloanAmount.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
            theloanAmount = theloanAmount.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
            theloanAmount = theloanAmount.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
            this.money = theloanAmount;
            $("#moneyInput").val(theloanAmount);
            if(parseFloat(this.money)>0){
                this.noSaoma = false;
            }else{
                this.noSaoma = true;
            }
        },
        saoma(){
            if (this.noSaoma) {
                this.$store.state.errorMsg = "请输入消费金额";

                return;
            }else{
                if (parseFloat(this.money) >= 1000000) {
                    this.$store.state.errorMsg = "消费金额不能大于100万";
                    return;
                } else if (parseFloat(this.money) < 1) {
                    this.$store.state.errorMsg = "消费金额不能小于1元";
                    return;
                }
                this.$store.state.showLoading = true;
                this.getWxScanQRCode();
//                this.getInstServlet();
            }
        },
        getWxScanQRCode() { // 调微信扫码功能
            var _this = this;
            wx.scanQRCode({
                needResult: 1,
                desc: 'scanQRCode desc',
                success: function(res) {
                    // 扫码后获取结果参数:htpp://xxx.com/c/?6123，截取到url中的防伪码后，赋值给Input
                    var stringcode = res.resultStr.substr(-18);
                    _this.getInstServlet(stringcode);

                },
                fail: function() {
                    _this.$store.state.showLoading = false;
                    _this.$store.state.errorMsg = "扫码失败,请在页面刷新后重新扫码";
                    setTimeout(function () {
                      location.reload()
                    },3000)
                },
                cancel: function(res) {
                    _this.$store.state.showLoading = false;
                }
            });

        },
        // 请求消费接口
        getInstServlet(stringcode) {
            var parameters = {
                token: this.$store.state.token,   // token
                loginName: this.$store.state.loginName,  // 商户号 或营业员手机号
                openid: this.$store.state.openid,  // 微信openid
                loginType: this.$store.state.loginType, // 登入类型 1表示商户 2表示营业员
                latitude: this.latitude,  // 经度
                longitude: this.longitude,  // 维度
                serial_no: stringcode,  // 订单串号
                tl_mer_no: this.$store.state.mer_id,  // 商户mer_id
                goods_amount: this.money,  // 本金
                prdt_no:"0301"   // 产品号只有联想通商通兑有
            };
            var saoma_URL = InstServlet_URL;
            if (this.$store.state.loginType == "1") {
                saoma_URL = MerInstServlet_URL;
            }
            var _this = this;
            $.post(saoma_URL, parameters, function xfback(msg) {
                console.dir(msg);
                _this.$store.state.showLoading = false;
                if (msg.login_response) {
                    _this.haslogin(msg);
                } else if (msg.xtsd_OrderInfo_response && msg.xtsd_OrderInfo_response.respCode == "0000") {
                    _this.money = "";
                    _this.$store.state.errorMsg = "交易成功";
                    $("#moneyInput").val("");
//                    location.href = "./success.html?fromPage=" + lastPage + "&money=" + $money.val();
                } else if (msg.xtsd_OrderInfo_response && msg.xtsd_OrderInfo_response.respCode == "0112") {
                    _this.staffSign = true;
                } else if (msg.xtsd_OrderInfo_response && msg.xtsd_OrderInfo_response.respCode && msg.xtsd_OrderInfo_response.resp_msg) {
                     _this.$store.state.errorMsg = msg.xtsd_OrderInfo_response.respCode + ":"+msg.xtsd_OrderInfo_response.respMsg;
                }else if(msg.xtsd_OrderInfo_response && msg.xtsd_OrderInfo_response && msg.xtsd_OrderInfo_response.respMsg){
                    _this.$store.state.errorMsg = msg.xtsd_OrderInfo_response.respMsg
                } else {
                    _this.$store.state.errorMsg = "支付失败";
                }
            });

        },
        closeSign(){
            this.staffSign = false;
        },
        signAgain() { // 消费商户签到
            var _this = this;
            this.staffSign = false;
            this.$store.state.showLoading = true;
            $.post(
                SignServletUrl, {
                    token: this.$store.state.token,   // token
                    loginName: this.$store.state.loginName,  // 商户号 或营业员手机号
                    openid: this.$store.state.openid,  // 微信openid
                    loginType: this.$store.state.loginType, // 登入类型 1表示商户 2表示营业员
                    latitude: this.latitude,  // 经度
                    longitude: this.longitude,  // 维度
                    state: "1"
                },
                function signAgainCallBack(saData) {
                    console.dir(saData);
                    _this.$store.state.showLoading = false;
                    if (saData.sign_response && saData.sign_response.resp_code == "0000") {
                        _this.$store.state.errorMsg ="签到成功";
                    } else {
                        _this.$store.state.errorMsg ="签到失败";
                    }
                });
        },
        // 配置成功获取经纬度
        getLocation() {
            var _this = this;
            wx.getLocation({
                type: "gcj02",
                success: function(res) {
                    console.log("getLocation data");
                    console.dir(res);
                    _this.latitude = res.latitude;
                    _this.longitude = res.longitude;
                    //        alert("当前地理位置：经度 " + latitude + "纬度 " + longitude);

                },
                fail: function() {
                    console.log("获取地理位置失败,请确认是否打开地理位置定位功能并授权微信获得地理位置权限");
                },
                cancel: function(res) {
                    console.log('用户拒绝授权获取地理位置');
                }
            });

        }
    },
    mounted:function  () {
        this.getLocation();
        var isIphone = cm.ismobile();
        if(isIphone == "0"){
          $("#moneyInput").attr("type","number");
        }
    }
}
</script>

<style lang='scss' scoped>
.page__bd_spacing {
    padding: 0 15px;
    margin-top: 30px;
}
</style>
